import $C from './config.js';
export default {
	common: {
		baseUrl: $C.baseUrl,
		header: {
			'Content-Type': 'application/json',
		},
		data: {},
		timeout: 30000,
		method: 'POST',
		dataType: 'json',
	},
	request(options = {}) {
		// options.datalist.banmi_system = uni.getStorageSync('system');
		// options.datalist.banmi_uniPlatform = uni.getStorageSync('uniPlatform');
		// options.datalist.banmi_region = uni.getStorageSync('region');
		// options.datalist.banmi_model = uni.getStorageSync('model');
		// options.datalist.banmi_browserName = uni.getStorageSync('browserName');
		// options.datalist.banmi_platform = uni.getStorageSync('platform');
		// options.datalist.banmi_screenHeight = uni.getStorageSync('screenHeight');
		// options.datalist.banmi_deviceBrand = uni.getStorageSync('deviceBrand');
		// options.datalist.banmi_brand = uni.getStorageSync('brand');
		// options.datalist.banmi_appVersion = uni.getStorageSync('appVersion');
		// options.datalist.banmi_deviceModel = uni.getStorageSync('deviceModel');
		// options.datalist.banmi_model = uni.getStorageSync('model');
		// options.datalist.banmi_osAndroidAPILevel = uni.getStorageSync('osAndroidAPILevel');
		// options.datalist.banmi_romName = uni.getStorageSync('romName');
		// options.datalist.banmi_romVersion = uni.getStorageSync('romVersion');
		// options.datalist.banmi_uid = uni.getStorageSync('uid');
		// options.datalist.banmi_token = uni.getStorageSync('token')
		// options.datalist.invite_code = uni.getStorageSync('invite_code');
		// options.datalist.lat = uni.getStorageSync('latitude');
		// options.datalist.lng = uni.getStorageSync('longitude');
		// if (options.datalist.token) {
		// 	options.datalist.banmi_token = options.datalist.token
		// }
		// options.datalist.banmi_banmi_plugin = $C.banmi_plugin;

		if (uni.getStorageSync('platform') == 'H5') {
			var domain = window.location.protocol + "//" + window.location.host + '/';
			if (domain.indexOf("localhost:") == -1 && domain.indexOf("192.168") == -1) {
				this.common.baseUrl = domain;
			} else {
				this.common.baseUrl = $C.baseUrl;
			}
		} else {
			this.common.baseUrl = $C.baseUrl;
		}
		options.url = this.common.baseUrl + options.url
		options.header = options.header || this.common.header
		options.header.headerToken = uni.getStorageSync('Token')
		options.timeout = 18000
		options.data = options.datalist
		options.dataType = options.dataType || this.common.dataType
		// 请求
		return new Promise((res, rej) => {
			// 请求中...
			uni.request({
				...options,
				success: (result) => {
					//维护中
					if (result.data.system == 0) {
						uni.showModal({
							title: '提示',
							content: result.data.system_name,
							success: function(res) {

							}
						});
						return
					}
					if (result.data.status == 101) {
						uni.showModal({
							title: '提示',
							content: result.data.msg,
							success: function(res) {

							}
						});
						return
					}
					res(result)
				},
				fail: (error) => {
					uni.showToast({
						title: '请求错误',
						icon: "none"
					});
					uni.hideLoading()
					return rej(error)
				}
			});
		})
	},
	init_jingmo_login() { //静默登录
		// #ifdef H5
		let ua = navigator.userAgent.toLowerCase();
		if (uni.getStorageSync('token') || ua.match(/MicroMessenger/i) != "micromessenger") {
			return
		}
		let link = window.location.href;
		if (link.indexOf("?code") != -1) {
			return
		}
		this.login_init({}).then(ret => {
			this.settings = ret.settings;
			this.plugin_set = ret.plugin_set;
			let link = window.location.href;
			if (link.indexOf("?code") != -1) {
				link = uni.getStorageSync('link')
			} else {
				uni.setStorage({
					key: 'link',
					data: link
				});
			}
			let code = null;
			if (!this.settings) {
				return;
			}
			let appid = this.settings['apppid'];
			var http = window.location.protocol;
			var com = window.location.host;
			var url = encodeURIComponent(link);
			window.location.href =
				`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;

		})
		// #endif

	},
	is_login() {
		if (!uni.getStorageSync('token')) {
			uni.navigateTo({
				url: '/pages/banmi/user_login'
			})
			return false
		}
		return true
	},
	mp_baidu_login(data) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/Baidulogin/Login', 'app', {
				datalist: data
			}).then(ret => {
				uni.showToast({
					title: ret.data.msg,
					icon: "none"
				});
				if (ret.data.status == 401) {
					return
				}

				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				}
			})

		});
	},
	app_Apple_login(data) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/Applelogin/Login', 'app', {
				datalist: data
			}).then(ret => {


				uni.showToast({
					title: ret.data.msg,
					icon: "none"
				});

				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				}
			})

		});
	},
	mp_kuai_login(data) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/Kuaishoulogin/Login', 'app', {
				datalist: data
			}).then(ret => {

				uni.showToast({
					title: ret.data.msg,
					icon: "none"
				});
				uni.hideLoading()
				if (ret.data.status == 401) {
					return
				}

				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				}
			})

		});
	},
	app_wx_login(data) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/login/app_wx_login', 'app', {
				datalist: data
			}).then(ret => {

				uni.showToast({
					title: ret.data.msg,
					icon: "none"
				});
				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				}
			})

		});
	},
	app_version(data) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/app/version', 'app', {
				datalist: data,
			}).then(ret => {
				res(ret)
			})

		});
	},
	user_reg(data) {
		console.log("111: " + JSON.stringify(111));
		return new Promise((res, rej) => {
			this.post('/banmi.php/login/account_reg', 'app', {
				datalist: data,
			}).then(ret => {
				uni.showToast({
					title: ret.data.msg,
					icon: "none"
				});
				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				}
			})

		});
	},
	mp_wx_login_tel(data) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/login/mp_wx_login_tel', 'app', {
				datalist: data
			}).then(ret => {

				if (ret.data.status == 200 || ret.data.status == 201) {
					// this.login_ok(ret.data.data.user, ret.data, res);
					return ret
				} else {
					uni.showToast({
						title: ret.data.msg,
						icon: "none"
					});
				}
			})

		});
	},
	getPhoneNumber(data) {
		return new Promise((res, rej) => {
			if (!data.agr) {
				uni.showToast({
					title: '请勾选同意协议',
					icon: "none"
				});
				return;
			}
			if (data.errMsg == 'getPhoneNumber:fail user deny') {
				uni.showToast({
					title: '请同意微信手机号授权',
					icon: "none"
				});
				return;
			}
			wx.checkSession({
				success() {
					data.encryptedData
					res(data.wx_code);
				},
				fail() {
					wx.login({
						success(res) {
							if (res.code) {
								res(res.wx_code);
							}
						}
					})
				}
			})
		});
	},
	mp_wx_code() {
		return new Promise((res, rej) => {
			wx.login({
				success(ret) {
					if (ret.code) {
						res(ret.code);
					}
				}
			})
		});
	},
	get_imgCode(data) {
		return new Promise((res, rej) => {
			let RandomSixStr = ''
			for (let i = 0; i < 10; i++) {
				RandomSixStr += String(Math.floor(Math.random() * 10))
			}
			data.code_img_rand = data.code_img + '?id=' + RandomSixStr;
			data.v_id = RandomSixStr;
			res(data);

		});
	},
	tel_message(data) {
		return new Promise((res, rej) => {
			if (!data.agr && data.is_agr) {
				uni.showToast({
					title: '请勾选同意协议',
					icon: "none"
				});
				return;
			}
			if (!data.phone) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'error'
				});
				return;
			}
			this.post('/banmi.php/login/tel_message', 'app', {
				datalist: data
			}).then(ret => {
				uni.showToast({
					title: ret.data.msg,
					icon: 'none'
				});
				if (ret.data.status == 200) {
					res(200);
				}
			})
		});
	},
	login_init(data) {
		let RandomSixStr = ''
		for (let i = 0; i < 10; i++) {
			RandomSixStr += String(Math.floor(Math.random() * 10))
		}
		let requestParams = {
			id: RandomSixStr
		};
		return new Promise((res, rej) => {
			this.post('/banmi.php/login/login_init', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {

				requestParams.find = ret.data.data.find;
				requestParams.plugin_set = ret.data.data.plugin_set;
				requestParams.settings = ret.data.data.settings;
				requestParams.settings.reg_img_code = ret.data.reg_img_code;
				requestParams.settings.code_img_rand = ret.data.code_img + '?id=' + RandomSixStr;
				requestParams.settings.code_img = ret.data.code_img;
				requestParams.settings.v_id = RandomSixStr;
				res(requestParams);
			})
		});
	},
	mp_wx_login(requestParams) {
		return new Promise((res, rej) => {
			this.post('/mehrb/login/user/jscode2session?code=' + requestParams.code+'&inviteUserId='+requestParams.inviteUserId, 'app', {
				// token: false,
				// datalist: requestParams
			}).then(ret => {
				uni.hideLoading()
				if (ret.data.code == 0) {
					uni.showToast({
						title: '登录成功',
						icon: 'none',
						duration: 4000
					});
					uni.setStorageSync('Token', ret.data.data) //将token等存储本地
					res(ret.data.data);
				} else {
					uni.showToast({
						title: ret.data.msg,
						icon: 'none',
						duration: 4000
					});
				}
			})
		});
	},
	ali_login(requestParams) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/Alilogin/Login', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.hideLoading()
				if (ret.data.status == 401) {
					uni.showToast({
						title: ret.data.msg,
						icon: 'none'
					})
					return
				}
				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				} else {
					res(ret.data)
				}
			})
		});
	},
	qq_login(requestParams) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/Qqlogin/Login', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.hideLoading()

				if (ret.data.status == 401) {
					uni.showToast({
						title: ret.data.msg,
						icon: 'none'
					})
					return
				}

				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				} else {
					res(ret.data)
				}
			})
		});
	},
	douyin_login(requestParams) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/Doulogin/Login', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.hideLoading()
				if (ret.data.status == 401) {
					uni.showToast({
						title: ret.data.msg,
						icon: 'none'
					})
					return
				}
				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				} else {
					res(ret.data)
				}
			})
		});
	},
	//微信公众号登录
	h5_jingmo_weixin_login() {
		let link = window.location.href;
		let code = null;
		let temp = link.split("code=")[1];
		if (!temp) {
			return
		}
		code = temp.split("&")[0];

		let requestParams = {
			code: code,
			id: this.v_id,
			is_login: '静默登录'
		};
		this.h5_weixin_login(requestParams).then(ret => {
			if (ret == 40163) {
				return
			}

		})
	},
	h5_weixin_login(requestParams) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/login/h5_weixin_login', 'app', {
				datalist: requestParams
			}).then(ret => {
				//公众号
				if (requestParams.is_login == '静默登录') {
					var data = ret.data;
					var user = ret.data.data.user
					if (data.force_tel == 1 && user.tel == '') {
						// this.error('请完善资料');
						user['code'] = 100;
						return
					}
					if (data.force_face == 1 && user.face == '') {
						// this.error('请完善资料');
						user['force_face'] = data.force_face;
						user['code'] = 100;
						return
					}

					uni.setStorage({
						key: 'token',
						data: user.token
					});
					uni.setStorage({
						key: 'uid',
						data: user.uid
					});
					uni.setStorage({
						key: 'nickname',
						data: user.nickname
					});
					uni.setStorage({
						key: 'face',
						data: user.face
					});
					uni.$emit('banmi_login', {
						title: 1,
					});
					return
				}
				uni.showToast({
					title: ret.data.msg,
					icon: 'none',
					duration: 4000
				});

				if (ret.data.status == 40163) {
					res(ret.data.status);
					return
				}
				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				}

			})
		});
	},
	pwd_login(requestParams) {
		return new Promise((res, rej) => {
			this.post('banmi.php/login/pwd_login', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.showToast({
					title: ret.data.msg,
					icon: 'none',
					duration: 4000
				});
				if (ret.data.status == 200) {
					this.login_ok(ret.data.data.user, ret.data, res);
				}
			})
		});
	},
	app_tel_login(requestParams) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/login/app_tel_login', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.showToast({
					title: ret.data.msg,
					icon: 'none',
					duration: 4000
				});
				if (ret.data.status == 200 || ret.data.status == 201) {
					this.login_ok(ret.data.data.user, ret.data, res);
				}
			})
		})
	},
	error(title) {
		uni.showToast({
			title: title,
			icon: 'error'
		})
	},
	success(title) {
		uni.showToast({
			title: title,
			icon: 'none'
		})
	},
	login_ok(user, data, res) {

		user.force_tel = data.force_tel;
		user.force_face = data.force_face;
		user.img_url = data.img_url;
		var msg = '';
		if (data.force_tel == 1 && user.tel == '') {
			this.error('请完善资料');
			user['code'] = 100;
			res(user);
			return
		}
		if (data.force_face == 1 && user.face == '') {
			this.error('请完善资料');
			user['force_face'] = data.force_face;
			user['code'] = 100;
			res(user);
			return
		}
		this.success(data.msg);
		uni.setStorage({
			key: 'token',
			data: user.token
		});
		uni.setStorage({
			key: 'uid',
			data: user.uid
		});
		uni.setStorage({
			key: 'nickname',
			data: user.nickname
		});
		uni.setStorage({
			key: 'face',
			data: user.face
		});
		setTimeout(function() {
			uni.$emit('banmi_login', {
				title: 1,
			});
			uni.switchTab({
				url: '/pages/index/index'
			})
		}, 2000);
	},
	//分销
	// inviteview() {
	// 	return new Promise((res, rej) => {
	// 		this.post('/banmi.php/Invite/inviteview', 'app', {
	// 			datalist: {}
	// 		}).then(ret => {
	// 			if (ret.data.status == 200) {
	// 				var face = ret.data.data.user.face;
	// 				if (!face) {
	// 					ret.data.data.user.face = $C.baseUrl + '/attachment/face/face.png'
	// 				} else {
	// 					var is_face = face.indexOf("https") != -1;
	// 					if (!is_face) {
	// 						ret.data.data.user.face = ret.data.img_url + face;
	// 					}
	// 				}

	// 				res(ret.data);
	// 			}
	// 		})
	// 	});
	// },
	user_info(val) { //查询用户信息
		return new Promise((res, rej) => {
			this.post('/mehrb/login/user/queryUserInfo', '', {}).then(ret => {
				res(ret);
			})
		});
	},
	publi() { //全局参数
		return new Promise((res, rej) => {
			this.post('/mehrb/meplus/funds/getCongifItem', '', {}).then(ret => {
				res(ret);
			})
		});
	},
	user_msg(id) { //查询用户信息
		return new Promise((res, rej) => {
			this.post('/mehrb/meplus/funds/get/' + id, '', {}).then(ret => {
				res(ret);
			})
		});
	},
	edit_account(val) {
		return new Promise((res, rej) => {
			let requestParams = {
				account: val
			};
			this.post('/banmi.php/User/edit_account', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.showToast({
					title: ret.data.msg,
					icon: 'none'
				})
				if (ret.data.status == 200) {
					res(200);
				} else {
					res(0);
				}
			})
		});
	},
	edit_nickname(requestParams) {
		return new Promise((res, rej) => {
			this.post('/banmi.php/User/edit_nickname', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.showToast({
					title: ret.data.msg,
					icon: 'none'
				})
				if (ret.data.status == 200) {
					uni.$emit('banmi_nickname', {
						title: requestParams.nickname,
					});
					res(200);
				} else {
					res(0);
				}
			})
		});
	},


	edit_face(requestParams) {
		return new Promise((res, rej) => {

			this.post('/banmi.php/User/edit_face', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.showToast({
					title: ret.data.msg,
					icon: 'none'
				})
				if (ret.data.status == 200) {
					uni.$emit('banmi_face', {
						title: ret.data.img_url + requestParams.face,
					});
					res(ret.data.img_url + requestParams.face);
				} else {
					res(0);
				}
			})
		});
	},
	edit_Gender(val) {
		return new Promise((res, rej) => {
			let requestParams = {
				gender: val
			};
			this.post('/banmi.php/User/edit_gender', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.showToast({
					title: ret.data.msg,
					icon: 'none'
				})
				if (ret.data.status == 200) {
					res(200);
				} else {
					res(0);
				}
			})
		});
	},



	User_out() { //退出登录
		return new Promise((res, rej) => {
			let requestParams = {
				gender: 0
			};
			this.post('/banmi.php/User/user_out', 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				if (ret.data.status == 200) {
					res(ret.data.data);
				} else {
					res(0);
				}
			})
		});
	},
	//绑定手机号
	// User_app_account_tel(requestParams) {
	// 	return new Promise((res, rej) => {
	// 		var url = '/banmi.php/Login/app_account_tel';
	// 		if (requestParams.encryptedData) {
	// 			var url = '/banmi.php/Login/mp_wx_bind_tel';
	// 		}
	// 		this.post(url, 'app', {
	// 			token: false,
	// 			datalist: requestParams
	// 		}).then(ret => {
	// 			uni.showToast({
	// 				title: ret.data.msg,
	// 				icon: 'none'
	// 			});
	// 			res(ret);
	// 		})
	// 	});
	// },

	// post请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	// post请求
	// get(url, data = {}, options = {}) {
	// 	options.url = url
	// 	options.data = data
	// 	options.method = 'GET'
	// 	return this.request(options)
	// },



}